package com.wiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wiki.entity.Content;
import com.wiki.entity.Doc;
import com.wiki.entity.Doc;
import com.wiki.mapper.ContentMapper;
import com.wiki.mapper.DocMapper;
import com.wiki.mapper.DocMapper;
import com.wiki.service.ContentService;
import com.wiki.service.DocService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wiki.utils.CopyUtil;
import com.wiki.utils.SnowFlake;
import com.wiki.utils.commonRequest.doc.DocQueryReq;
import com.wiki.utils.commonRequest.doc.DocSaveReq;
import com.wiki.utils.commonResponse.DocQueryResp;
import com.wiki.utils.commonResponse.PageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文档 服务实现类
 * </p>
 *
 * @author 偷闰土的猹
 * @since 2022-01-07
 */
@Service
public class DocServiceImpl extends ServiceImpl<DocMapper, Doc> implements DocService {


    @Autowired
    private DocMapper docMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Autowired
    private ContentMapper contentMapper;

    @Override
    public PageResp<DocQueryResp> listAll(DocQueryReq req) {

        PageResp<DocQueryResp> respList = new PageResp<>();
        if (req.getPage() != 0 && req.getSize() != 0) {
            PageHelper.startPage(req.getPage(), req.getSize());
        }

        List<Doc> docs = list();
        PageInfo<Doc> pageInfo = new PageInfo<>(docs);
        respList.setTotal(pageInfo.getTotal());
        List<DocQueryResp> respList1 = CopyUtil.copyList(docs, DocQueryResp.class);
        respList.setList(respList1);

        return respList;
    }

    @Override
    public void save(DocSaveReq req) {

        Doc copy = CopyUtil.copy(req, Doc.class);
        Content content = CopyUtil.copy(req,Content.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            //新增
            Long id = snowFlake.nextId();
            copy.setId(id);
            content.setId(id);
            docMapper.insert(copy);
            contentMapper.insert(content);
        } else {
            //更新
            docMapper.updateById(copy);
            int i = contentMapper.updateById(content);
            if(i==0){
                contentMapper.insert(content);
            }

        }
    }

    @Override
    public void delete(List<Long> id) {
       docMapper.deleteBatchIds(id);
    }

    @Override
    public PageResp<DocQueryResp> queryLike(DocQueryReq req) {
        PageResp<DocQueryResp> respList = new PageResp<>();
        PageHelper.startPage(req.getPage(), req.getSize());
//        QueryWrapper<Doc> wrapper = new QueryWrapper<Doc>().like("name",req.getName());
        List<Doc> docs = docMapper.selectList(null);
        PageInfo<Doc> pageInfo = new PageInfo<>(docs);
        respList.setTotal(pageInfo.getTotal());
        List<DocQueryResp> respList1 = CopyUtil.copyList(docs, DocQueryResp.class);
        respList.setList(respList1);

        return respList;
    }

    @Override
    public List<DocQueryResp> all(Long ebookId) {
        QueryWrapper<Doc> wrapper = new QueryWrapper<Doc>().orderByAsc("sort").eq("ebook_id",ebookId);
        List<Doc> categories = docMapper.selectList(wrapper);

        List<DocQueryResp> docQueryResps = CopyUtil.copyList(categories, DocQueryResp.class);
        return docQueryResps;
    }

    @Override
    public String findContent(Long id) {
        Content content = contentMapper.selectById(id);
        if(content!=null)
            return content.getContent();
        return "";
    }

}
